
local skynet = require "skynet"
local utils = require "utils"
local dbutils = require "dbutils"
local snax = require "skynet.snax"
local sharedata = require "skynet.sharedata"

local data = {}
local roledata = {}
local db_item

local itemIdCount = 10000
--[[ 
    {
        uid
        configId
        roldId
        num
    }
 ]]

function init()
    local ret = dbutils.loaddataforkey("db_item","max(uid)")
    if #ret > 0 then
        local v = ret[1]
        if v["max(uid)"] then
            -- body
            itemIdCount = tonumber(v["max(uid)"])
        end
    end
    skynet.error(itemIdCount)

    db_item = sharedata.query("item")
end

function exit()

end

local function getItem( uid )
    return data[uid]
end

local function getItemList( roldId )
    if roledata[roldId] == nil then
        roledata[roldId] = {}
    end
    return roledata[roldId]
end

local function createItem(rid,cid,num)
    cid = tonumber(cid)
    num = num or 1
    itemIdCount = itemIdCount + 1
    local item = {
        uid = itemIdCount,
        configId = cid,
        roleId = rid,
        num = num
    }

    local list = getItemList(rid)

    list[cid] = item
    data[itemIdCount] = item
    
    dbutils.insertdata("db_item",item)
    return item
end

local function addItem(roleid,configid,num)
    configid = tonumber(configid)
    num = num or 1
    if configid == 1001 then
        -- local gold = usermgr.req.addGold(roleid,num)
        return nil
    end

    -- local itemcfg = db_item[tostring(configid)]
    -- if itemcfg and itemcfg.type == 5 then
    --     return nil
    -- end
    local list = getItemList(roleid)
    local item = list[configid]
    if item then
        item.num = item.num + num
        dbutils.updatedata("db_item",{
            uid = item.uid,
            num = item.num
        },item.uid)
    else
        if num > 0 then
            item = createItem(roleid,configid,num)
        end
    end
    return item
end



local function removeItem(uid)
    local item = data[uid]
    if item then
        -- LOG_ERROR("removeItem ",uid)
        if item.roleId then
            local list = getItemList(item.roleId)
            list[item.configId] = nil
        end
    end
    data[uid] = nil
    dbutils.removedata("db_item",uid)
end


function response.dataload(roldId)
    if roledata[roldId] then
        return 
    end

    local ret = dbutils.loaddataforkey("db_item","*","roleId="..roldId)
    roledata[roldId] = {}                        
    if #ret > 0 then
        for k,v in pairs(ret) do
            roledata[roldId][v.configId] = v
            data[v.uid] = v
        end
    end
end

function response.getItem(uid)
    return getItem(uid)
end

function response.getItemList(roldId)
    return getItemList(roldId)
end

function response.getItemForConf(roldId,confid)
    confid = tonumber(confid)
    local list = getItemList(roldId)
    return list[confid]
end

function response.unload(roleid)
    local list = roledata[roleid]
    if list then
        for k,v in pairs(list) do
            data[k] = nil
        end
    end
    roledata[roleid] = nil
end

function response.updateItem(item)
    local temp = getItem(item.uid)
    if temp then
        temp.num = item.num
        if temp.num == 0 then
            removeItem(temp.uid)
        else
            dbutils.updatedata("db_item",{
                uid = temp.uid,
                num = temp.num
            },temp.uid)
        end
    end
end

function response.addItem(roleid,configid,num)
    return addItem(roleid,configid,num)
end

function response.removeItem(uid)
    removeItem(uid)
end
